package com.lzk.auth.service.impl;

import com.lzk.auth.service.SysMenuService;
import com.lzk.auth.service.SysUserService;
import com.lzk.model.system.SysUser;
import com.lzk.security.custom.CustomUser;
import com.lzk.security.custom.UserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUser sysUser = sysUserService.getUserByUsername(username);

        if (null == sysUser) {
            throw new UsernameNotFoundException("用户名不存在！");
        }


        List<String> userPermsList = sysMenuService.findUserPermsByUserId(sysUser.getId());
        List<SimpleGrantedAuthority>authList=new ArrayList<>();
        for (String perms : userPermsList) {
            authList.add(new SimpleGrantedAuthority(perms.trim()));

        }

        return new CustomUser(sysUser, authList);
    }



}
